package com.piggy.settlement.sdk.client.singlewithdraw;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.PiggySdkConstants;
import com.piggy.settlement.sdk.constant.PiggySingleWithdrawalConstants;
import com.piggy.settlement.sdk.util.PiggyVerifyUtil;
import com.piggy.settlement.sdk.util.PiggyXmlUtil;

/**
 * @author ：daxia
 * @description：单笔提现相关接口
 * @modified By：
 * @version: 1.0$
 */
public class PiggySingleWithdrawalApiClient {

    static PiggySingleWithdrawalApiClient piggySingleWithdrawalApiClient = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggySingleWithdrawalApiClient(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggySingleWithdrawalApiClient getInstanse(String appSecret, String domain) {
        if (piggySingleWithdrawalApiClient == null) {
            piggySingleWithdrawalApiClient =
                    new PiggySingleWithdrawalApiClient(appSecret, domain);
        }

        return piggySingleWithdrawalApiClient;
    }

    /**
     * 单笔提现上报-普通
     * 该方法已经过时，请使用v2相关接口- singleSubmit
     *
     * @param signleVo 参数实体：
     * @return
     */
    @Deprecated
    public JSONObject singleWithdrawal(Object signleVo) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signleVo);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(signleVo);

            String xml = PiggyXmlUtil.objectToXml(signleVo);
            String response = HttpRequest.post(domain + PiggySingleWithdrawalConstants.SINGLE_WITHDRAWAL_V1 + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪单笔提现上报发生错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 单笔提现上报-特殊
     * 该方法已经过时，请使用v2相关接口- singleSubmitLimit
     *
     * @param signleVo 参数实体
     * @return
     */
    @Deprecated
    public JSONObject singleWithdrawalLimit(Object signleVo) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signleVo);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(signleVo);

            String xml = PiggyXmlUtil.objectToXml(signleVo);
            String response = HttpRequest.post(domain + PiggySingleWithdrawalConstants.SINGLE_WITHDRAWAL_LIMIT_V1 + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("小猪单笔提现上报发生错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 单笔提现上报结果查询
     * 该方法已经过时，请使用v2相关接口- querySingleTrade
     *
     * @param signleVo 参数实体
     * @return
     */
    @Deprecated
    public JSONObject singleWithdrawalQuery(Object signleVo) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signleVo);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(signleVo);

            String xml = PiggyXmlUtil.objectToXml(signleVo);
            String response = HttpRequest.post(domain + PiggySingleWithdrawalConstants.GET_WITHDRAWAL_DETAIL + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("获取小猪单笔提现上报结果发生错误");
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 实时提现查询对账明细
     *
     * @param signleVo 参数实体
     * @return
     */
    @Deprecated
    public JSONObject getWithdrawReconcileTradesDetail(Object signleVo) {

        try {
            String signStr = PiggyVerifyUtil.getSigStr(appSecret, signleVo);
            String urlSignStr = PiggyVerifyUtil.buildMapToSign(signleVo);

            String xml = PiggyXmlUtil.objectToXml(signleVo);
            String response = HttpRequest.post(domain + PiggySingleWithdrawalConstants.GET_WITHDRAW_RECONCILE_TRADES_DETAIL + "?" + urlSignStr)
                    .header("sig", signStr)
                    .body(xml)
                    .timeout(20000)
                    .execute()
                    .body();

            return getJsonObject(response);
        } catch (Exception e) {
            System.out.println("获取小猪实时提现查询对账明细发生错误");
            e.printStackTrace();
            return null;
        }

    }

    private static JSONObject getJsonObject(String response) {
        if (!StrUtil.hasBlank(response) && response.startsWith(PiggySdkConstants.JSON_STR_START)) {
            return JSONUtil.parseObj(response);
        } else {
            return JSONUtil.parseFromXml(response);
        }
    }

}
